// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   Querylet.java

package com.vandagroup.common.po;

import com.vandagroup.common.bo.DataBean;
import com.vandagroup.common.bo.ESSObject;
import com.vandagroup.common.db.ESSDataStoreManager;
import com.vandagroup.common.exp.ESSConnectionGetTimeOutException;
import com.vandagroup.common.util.Result;
import java.sql.*;
import java.util.Vector;

// Referenced classes of package com.vandagroup.common.po:
//            PortalPO

public class Querylet extends ESSObject
{

    protected int resultSize;
    ResultSet rs;
    Connection conn;
    Statement stmt;

    public Querylet()
    {
        resultSize = 20;
        rs = null;
        conn = null;
        stmt = null;
    }

    public void getStatement()
        throws SQLException, ESSConnectionGetTimeOutException
    {
        if(conn == null)
            conn = ESSObject.getDataStore().getConnection();
        if(stmt == null)
            stmt = conn.createStatement();
    }

    public ResultSet retrieve(String sql)
        throws SQLException, ESSConnectionGetTimeOutException
    {
        try
        {
            getStatement();
            rs = stmt.executeQuery(sql);
            return rs;
        }
        catch(SQLException ex)
        {
            log(ex.getMessage());
            freeAll();
            throw ex;
        }
    }

    public Result retrieve(String sql, int page)
        throws SQLException, ESSConnectionGetTimeOutException
    {
        return retrieve(sql, page, true);
    }

    public Result retrieve(String sql, int page, boolean countable)
        throws SQLException, ESSConnectionGetTimeOutException
    {
        Result rh;
        DataBean data;
        int currsorPosition;
        rh = new Result();
        rh.setResultSize(resultSize);
        data = new DataBean();
        currsorPosition = 0;
        int startPosition = rh.getStartPosition(page);
        if(page < 1)
            resultSize = 0;
        try
        {
            getStatement();
            rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            int cols = rsmd.getColumnCount();
            rsmd = null;
            while(rs.next()) 
            {
                if(currsorPosition < startPosition)
                {
                    currsorPosition++;
                    continue;
                }
                if(resultSize > 0 && currsorPosition - startPosition == resultSize)
                {
                    currsorPosition++;
                    break;
                }
                DataBean bean = new DataBean();
                PortalPO po = new PortalPO();
                for(int i = 1; i <= cols; i++)
                    bean.addObject(po.convert(rs.getObject(i)));

                data.addObject(bean);
                currsorPosition++;
            }
            if(countable)
                while(rs.next()) 
                    currsorPosition++;
        }
        finally
        {
            freeAll();
        }
        rh.setBeans(data);
        rh.setCounter(currsorPosition);
        return rh;
    }

    public Vector retrieveList(String sql)
        throws SQLException, ESSConnectionGetTimeOutException
    {
        Vector data;
        data = new Vector();
        try
        {
            getStatement();
            rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            int cols = rsmd.getColumnCount();
            rsmd = null;
            Vector row = null;
            for(; rs.next(); data.addElement(row))
            {
                row = new Vector();
                for(int i = 1; i <= cols; i++)
                    row.addElement(rs.getObject(i));

            }

        }
        finally
        {
            freeAll();
        }
        return data;
    }

    public void freeAll()
        throws SQLException
    {
        try
        {
            if(rs != null)
                rs.close();
            if(stmt != null)
                stmt.close();
            rs = null;
            stmt = null;
        }
        finally
        {
            if(conn != null)
                ESSObject.getDataStore().freeConnection(conn);
            conn = null;
        }
        return;
    }

    public void setResultSize(int newResultSize)
    {
        resultSize = newResultSize;
    }

    public int getResultSize()
    {
        return resultSize;
    }
}
